package com.facebook 
{
	import com.facebook.Events.FacebookEvent;
	import com.facebook.commands.FacebookCall;
	import com.facebook.utils.FacebookConnectUtils;

	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.external.ExternalInterface;

	/**
	 * @author Ben Smith
	 */
	public class FaceBookSession extends EventDispatcher 
	{

		//	private var _parser:XML

		private var _uid : String;
		private var _secret : String;
		private var session_key : String;
		private var _expires : String;
		private var base_domain : String;
		private var access_token : String;
		private var _sig : String;
		private var _permissions : Array;
		private var _status : String;

		
		
		public function FaceBookSession(target : IEventDispatcher = null)
		{
			super(target);
			_permissions = new Array();
		}

		public function getStatus() : void
		{
			
			var faceBook : FacebookCall = new FacebookCall();
			var _jsCall : String = 'FB.getLoginStatus' + '(' + FacebookConnectUtils.createAnonymousCallFunction(); 
			FacebookConnectUtils.registerCall(faceBook);
			faceBook.addEventListener(FacebookEvent.COMPLETE, onSessionFound);
			ExternalInterface.call(_jsCall);
		}

		private function onSessionFound(event : FacebookEvent) : void 
		{
			
			
			
			var target : FacebookCall = event.target as FacebookCall;
			target.removeEventListener(FacebookEvent.COMPLETE, onSessionFound);
			var response : Object = event.data.result as Object;
			
		
			_status = response['status'];
			var permissionsAccepted : Object = response['perms'];
			
			for (var i:* in permissionsAccepted) {
				_permissions[_permissions.length] = permissionsAccepted[i];
			}
			
			var withinSession : Object = response['session'];
			
			for(var j:String in withinSession) {
				this[j]=withinSession[j]
			}
			dispatchEvent(new FacebookEvent(FacebookEvent.VERIFYING_SESSION));
		}

		
		public function acceptSession(session : Object) : void
		{
		}

		public function get permissions() : Array {
			return _permissions;
		}
		
		public function get baseDomain() : String {
			return base_domain;
		}

		public function get uid() : String {
			return _uid;
		}

		public function get secret() : String {
			return _secret;
		}

		public function get  sessionKey() : String {
			return session_key;
		}

		public function get expires() : String {
			return _expires;
		}

		public function get  accessToken() : String {
			return access_token;
		}

		public function get sig() : String {
			return _sig;
		}

		public function get status() : String {
			return _status;
		}
		
		public function set uid(uid : String) : void {
			_uid = uid;
		}
		
		public function set secret(secret : String) : void {
			_secret = secret;
		}
		
		public function set expires(expires : String) : void {
			_expires = expires;
		}
		
		public function set sig(sig : String) : void {
			_sig = sig;
		}
		
		public function set permissions(permissions : Array) : void {
			_permissions = permissions;
		}
		
		public function set status(status : String) : void {
			_status = status;
		}
	}
}
